package com.xxkj.drugtraceability.ui.tracdetail

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.hjq.shape.layout.ShapeLinearLayout
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DrugInfoBean
import com.xxkj.drugtraceability.databinding.*
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class TracDetailAdapter(private val actionClick:(index:Int)->Unit,private val saveAction:(item: DrugInfoBean)->Unit,private val upAction:(item: DrugInfoBean)->Unit):BaseQuickAdapter<DrugInfoBean, BaseDataBindingHolder<LayoutDoctoradvicebackBinding>>(R.layout.layout_doctoradviceback) {
    private var scan = false
    private var isRollBackTrade = false
    fun setCanScan(scan:Boolean){
        this.scan = scan
    }
    fun setRollBackTrade(isRollBackTrade:Boolean){
        this.isRollBackTrade = isRollBackTrade
    }
    override fun convert(
        holder: BaseDataBindingHolder<LayoutDoctoradvicebackBinding>,
        item: DrugInfoBean
    ) {
        holder.dataBinding?.apply {
            val buffer = StringBuffer()
            buffer.append("<font color='#33333F' size='50px'>")
            buffer.append("<b>"+item.drugName+"    "+"</b>")
            buffer.append("</font>")
            buffer.append("<font color='#494953' size='24px'>")
            buffer.append(item.drugSpec?:"")
            buffer.append("</font>")
            if (item.trdnFlag == "1") {
                //当前药品是否是拆零标识  1是 0否
                try {
                    buffer.append("<img src='" + R.mipmap.icon_unpack + "'/>")
                    tvContent.text = Html.fromHtml(buffer.toString(), {
                        var drawable = ContextCompat.getDrawable(context, R.mipmap.icon_unpack)
                        drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
                        drawable!!
                    }, null)
                } catch (e: Exception) {
                    tvContent.text = Html.fromHtml(buffer.toString())
                }
            } else {
                tvContent.text = Html.fromHtml(buffer.toString())
            }
            if ("1"==item.drugUploadFlag){
                //已上传
                scan = false
                tvState.text = "已上传"
                tvState.setBackgroundResource(R.drawable.item_upload)
                tvState.setTextColor(context.resources.getColor(R.color.color_1fb57f))
                btnUp.visibility = View.GONE
                btnSave.visibility = View.GONE
            }else{
                scan = true
                tvState.text = "未上传"
                tvState.setBackgroundResource(R.drawable.item_unupload)
                tvState.setTextColor(context.resources.getColor(R.color.color_orange))
                btnUp.visibility = View.VISIBLE
                btnSave.visibility = View.VISIBLE
                btnUp.clickN {
                    //要上传
                    upAction.invoke(item)
                }
                btnSave.clickN {
                    //修改追溯码
                    saveAction.invoke(item)
                }
            }
            llAdd.removeAllViews()
                //被选中
             llParent.isSelected = item.isSelected
            llParent.setOnClickListener {
                item.isSelected = !item.isSelected
                if (item.isSelected){
                    actionClick.invoke(getItemPosition(item))
                }else{
                    actionClick.invoke(-1)
                }
                notifyDataSetChanged()
            }
            if (item.drugTracInfoList==null){
                item.drugTracInfoList = mutableListOf()
            }
            if (item.drugTracInfoList.isNullOrEmpty()){
                llBottom.visibility = View.GONE
            }else{
                llBottom.visibility = View.VISIBLE
                item.drugTracInfoList?.forEach { drugTrace->
                    val viewDrug =  LayoutInflater.from(context).inflate(R.layout.layout_drug_scan_2,null)
                    val bind = DataBindingUtil.bind<LayoutDrugScan2Binding>(viewDrug)
                    bind?.apply {
                        data =drugTrace.drugTracCodg
                        llMore.visibility = View.GONE
                        if (scan){
                            ivDelete.visibility = View.VISIBLE
                            ivDelete.tag = viewDrug
                            ivDelete.clickN {
                                //删除
                                item.drugTracInfoList?.remove(drugTrace)
                                llAdd.removeView(ivDelete.tag as View)
                            }
                            if (item.trdnFlag == "1") {
                                etDrugNum.visibility = View.VISIBLE
                                if (drugTrace.drugTracCodeNum != null) {
                                    etDrugNum.setText(drugTrace.drugTracCodeNum.toString())
                                }
                                etDrugNum.doAfterTextChanged {
                                    try {
                                        drugTrace.drugTracCodeNum = Integer(it!!.toString())
                                    } catch (e: Exception) {
                                    }
                                }
                                etDrugNum.setOnFocusChangeListener { _, hasFocus ->
                                    if (!hasFocus && etDrugNum.text.isNullOrEmpty()) {
                                        drugTrace.drugTracCodeNum = Integer(1)
                                        etDrugNum.setText("1")
                                    }
                                }
                                if (isRollBackTrade) {
                                    etDrugNum.isEnabled = false
                                }
                            } else {
                                drugTrace.drugTracCodeNum = Integer(1)
                            }
                        }else{
                            ivDelete.visibility = View.INVISIBLE
                            if (item.trdnFlag == "1" && drugTrace.drugTracCodeNum != null) {
                                etDrugNum.visibility = View.VISIBLE
                                etDrugNum.isEnabled = false
                                etDrugNum.setText(drugTrace.drugTracCodeNum.toString())
                            }
                        }

                        llAdd.addView(viewDrug)
                    }
                }

            }
        }

        }
}